﻿Imports System.Drawing.Drawing2D
Imports System.ComponentModel

Public Class PTIC_RadioButton

#Region "Enumérations"

    Public Enum nomThemeRadioButton
        OMANE
        Standard
    End Enum

#End Region

#Region "Variables de classe"

#Region "Choix du thème"
    Private _theme As ThemeComposant
    Private _nomTheme As nomThemeRadioButton
#End Region

#Region "Personnalisation du thème"

    Private _borderPerso As Boolean
    Private _BorderColorNonChecked_Left As Color
    Private _BorderColorNonChecked_top As Color
    Private _BorderColorNonChecked_Right As Color
    Private _BorderColorNonChecked_Bottom As Color
    Private _BorderColorChecked_Left As Color
    Private _BorderColorChecked_top As Color
    Private _BorderColorChecked_Right As Color
    Private _BorderColorChecked_Bottom As Color

    Private _BackColorEstDegrade As Boolean
    Private _BackColorAxeDegrade As Double
    Private _BackColorDegradeChecked11 As Color
    Private _BackColorDegradeChecked12 As Color
    Private _BackColorDegradeChecked21 As Color
    Private _BackColorDegradeChecked22 As Color
    Private _BackColorDegradeNonChecked11 As Color
    Private _BackColorDegradeNonChecked12 As Color
    Private _BackColorDegradeNonChecked21 As Color
    Private _BackColorDegradeNonChecked22 As Color

#End Region

#Region "Gestion de l'icône"
    Private _ImageHeight As Integer
    Private _ImagePersoHeight As Boolean
#End Region

#End Region

#Region "Propriétés"

#Region "Choix du thème"

    <Browsable(True), Category("Thème")> _
     Public Property theme() As nomThemeRadioButton
        Get
            Return _nomTheme
        End Get
        Set(ByVal value As nomThemeRadioButton)

            _nomTheme = value

            Select Case value

                Case nomThemeRadioButton.OMANE

                    Dim etat As etatComposant
                    If Me.Checked Then etat = etatComposant.MouseOver Else etat = etatComposant.Enabled

                    Dim styleOMANESTD As _
                            New styleEtatComposant(My.Settings.OMANERdbSTD_ColorDegrade1, _
                                                   My.Settings.OMANERdbSTD_ColorDegrade2, _
                                                   My.Settings.OMANERdbSTD_ColorDegrade3, _
                                                   My.Settings.OMANERdbSTD_ColorDegrade4, _
                                                   Me.Font, _
                                                   My.Settings.OMANE_STDForeColor, _
                                                   My.Settings.OMANE_ColorBorder, _
                                                   My.Settings.OMANE_AngleRoundRect)
                    Dim styleOMANEMOUSEOVER As _
                            New styleEtatComposant(My.Settings.OMANERdbMOUSEOVER_ColorDegrade1, _
                                                   My.Settings.OMANERdbMOUSEOVER_ColorDegrade2, _
                                                   My.Settings.OMANERdbMOUSEOVER_ColorDegrade3, _
                                                   My.Settings.OMANERdbMOUSEOVER_ColorDegrade4, _
                                                   Me.Font, _
                                                   My.Settings.OMANE_MOUSEOVERForeColor, _
                                                   My.Settings.OMANE_ColorBorder, _
                                                   My.Settings.OMANE_AngleRoundRect)
                    Dim styleOMANEDIS As _
                            New styleEtatComposant(My.Settings.OMANERdbSTD_ColorDegrade1, _
                                                   My.Settings.OMANERdbSTD_ColorDegrade2, _
                                                   My.Settings.OMANERdbSTD_ColorDegrade3, _
                                                   My.Settings.OMANERdbSTD_ColorDegrade4, _
                                                   Me.Font, _
                                                   My.Settings.OMANE_STDForeColor, _
                                                   My.Settings.OMANE_ColorBorder, _
                                                   My.Settings.OMANE_AngleRoundRect)

                    _theme = New ThemeComposant(styleOMANESTD, _
                                                styleOMANEMOUSEOVER, _
                                                styleOMANEDIS, etat)


            End Select
            Invalidate(True)
        End Set
    End Property

#End Region

#Region "Personnalisation du thème"
    'Propriété relative au dessin de l'icone
    Public Property ImagePersoHeight() As Boolean
        Get
            Return _ImagePersoHeight
        End Get
        Set(ByVal value As Boolean)
            _ImagePersoHeight = value
        End Set
    End Property
    Public Property ImageHeight() As Integer
        Get
            Return _ImageHeight
        End Get
        Set(ByVal value As Integer)
            Me._ImageHeight = value
        End Set
    End Property
    '----- Propriétés relatives à la personnalisation des bordures du boutton -----
    'Faut il personnaliser les bordures
    Public Property BorderPerso() As Boolean
        Get
            Return _borderPerso
        End Get
        Set(ByVal value As Boolean)
            Me._borderPerso = value
        End Set
    End Property
    'Bordure gauche du panel
    Public Property BorderColorNonChecked_Left() As Color
        Get
            Return _BorderColorNonChecked_Left
        End Get
        Set(ByVal value As Color)
            Me._BorderColorNonChecked_Left = value
        End Set
    End Property
    'Bordure haut du panel
    Public Property BorderColorNonChecked_top() As Color
        Get
            Return _BorderColorNonChecked_top
        End Get
        Set(ByVal value As Color)
            Me._BorderColorNonChecked_top = value
        End Set
    End Property
    'Bordure droite du panel
    Public Property BorderColorNonChecked_right() As Color
        Get
            Return _BorderColorNonChecked_Right
        End Get
        Set(ByVal value As Color)
            Me._BorderColorNonChecked_Right = value
        End Set
    End Property
    'Bordure bas du panel
    Public Property BorderColorNonChecked_bottom() As Color
        Get
            Return _BorderColorNonChecked_Bottom
        End Get
        Set(ByVal value As Color)
            Me._BorderColorNonChecked_Bottom = value
        End Set
    End Property
    Public Property BorderColorChecked_Left() As Color
        Get
            Return _BorderColorChecked_Left
        End Get
        Set(ByVal value As Color)
            Me._BorderColorChecked_Left = value
        End Set
    End Property
    'Bordure haut du panel
    Public Property BorderColorChecked_top() As Color
        Get
            Return _BorderColorChecked_top
        End Get
        Set(ByVal value As Color)
            Me._BorderColorChecked_top = value
        End Set
    End Property
    'Bordure droite du panel
    Public Property BorderColorChecked_right() As Color
        Get
            Return _BorderColorChecked_Right
        End Get
        Set(ByVal value As Color)
            Me._BorderColorChecked_Right = value
        End Set
    End Property
    'Bordure bas du panel
    Public Property BorderColorChecked_bottom() As Color
        Get
            Return _BorderColorChecked_Bottom
        End Get
        Set(ByVal value As Color)
            Me._BorderColorChecked_Bottom = value
        End Set
    End Property
    '----- Propriétés relatives à l'arrière plan dégradé -----
    'Paramétrage de la présence ou non d'un dégradé
    Public Property BackColorEstDegrade() As Boolean
        Get
            Return _BackColorEstDegrade
        End Get
        Set(ByVal value As Boolean)
            Me._BackColorEstDegrade = value
        End Set
    End Property

    'Paramétrage de l'emplacement de l'axe du dégradé
    Public Property BackColorAxeDegrade() As Double
        Get
            Return _BackColorAxeDegrade
        End Get
        Set(ByVal value As Double)
            Me._BackColorAxeDegrade = value
        End Set
    End Property
    'Couleur 1 du dégradé en haut si ckecked
    Public Property BackColorDegradeChecked11() As Color
        Get
            Return _BackColorDegradeChecked11
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeChecked11 = value
        End Set
    End Property
    'Couleur 2 du dégradé en haut si ckecked
    Public Property BackColorDegradeChecked12() As Color
        Get
            Return _BackColorDegradeChecked12
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeChecked12 = value
        End Set
    End Property
    'Couleur 1 du dégradé en bas si ckecked
    Public Property BackColorDegradeChecked21() As Color
        Get
            Return _BackColorDegradeChecked21
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeChecked21 = value
        End Set
    End Property
    'Couleur 2 du dégradé en bas si ckecked
    Public Property BackColorDegradeChecked22() As Color
        Get
            Return _BackColorDegradeChecked22
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeChecked22 = value
        End Set
    End Property
    'Couleur 1 du dégradé en haut si non ckecked
    Public Property BackColorDegradeNonChecked11() As Color
        Get
            Return _BackColorDegradeNonChecked11
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeNonChecked11 = value
        End Set
    End Property
    'Couleur 2 du dégradé en haut si non ckecked
    Public Property BackColorDegradeNonChecked12() As Color
        Get
            Return _BackColorDegradeNonChecked12
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeNonChecked12 = value
        End Set
    End Property
    'Couleur 1 du dégradé en bas si non ckecked
    Public Property BackColorDegradeNonChecked21() As Color
        Get
            Return _BackColorDegradeNonChecked21
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeNonChecked21 = value
        End Set
    End Property
    'Couleur 2 du dégradé en bas si non ckecked
    Public Property BackColorDegradeNonChecked22() As Color
        Get
            Return _BackColorDegradeNonChecked22
        End Get
        Set(ByVal value As Color)
            Me._BackColorDegradeNonChecked22 = value
        End Set
    End Property
#End Region

#End Region

#Region "Constructeur"

    Public Sub New()

        InitializeComponent()

        'Activation du double buffering
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)

        _BackColorEstDegrade = False
        _borderPerso = False
        'If Not Me.Image Is Nothing Then
        ' initTailleImage()
        'End If

    End Sub

#End Region

#Region "Surcharges pour personnalisation du contrôle"

    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        If _ImagePersoHeight Then
            Dim tailleImage As RectangleF = Me.Image.GetBounds(GraphicsUnit.Display)
            Dim W1 As Integer = tailleImage.Width
            Dim H1 As Integer = tailleImage.Height
            Dim W2 As Integer = _ImageHeight
            Dim H2 As Integer = W2 * H1 / W1
            'et maintenant la transformation : 
            'Création d'un objet bitmap destiné à recevoir l'image transformée
            Dim bm_dest As New Bitmap(W2, H2)
            ' Creer un GraphicsOject pour le resultat du Bitmap.
            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
            'Copie l'image source dans le bitmap.
            gr_dest.DrawImage(Me.Image, 0, 0, W2, H2)
            Me.Image = bm_dest
            gr_dest.Dispose()
        End If
    End Sub

    Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)

        MyBase.OnPaint(pevent)

        'Récupération du contexte graphique du RadioButton
        Dim g As Graphics = pevent.Graphics

        If _nomTheme = nomThemeRadioButton.OMANE Then
            Styles.PaintDoubleDegrade(g, Me.ClientRectangle, _theme.styleEnCours.backColor, _
                                      _theme.styleEnCours.backColor2, _
                                      _theme.styleEnCours.backColor3, _
                                      _theme.styleEnCours.backColor4, 0.5)
        End If

        If _BackColorEstDegrade Then
            If Me.Checked = False Then
                Styles.PaintDoubleDegrade(g, Me.ClientRectangle, _BackColorDegradeNonChecked11, _
                                      _BackColorDegradeNonChecked12, _
                                      _BackColorDegradeNonChecked21, _
                                      _BackColorDegradeNonChecked21, _BackColorAxeDegrade)
            Else
                Styles.PaintDoubleDegrade(g, Me.ClientRectangle, _BackColorDegradeChecked21, _
                      _BackColorDegradeChecked22, _
                      _BackColorDegradeChecked11, _
                      _BackColorDegradeChecked12, _BackColorAxeDegrade)
            End If
        End If

        'Tracé du texte
        If _nomTheme = nomThemeRadioButton.OMANE Or _BackColorEstDegrade Then
            Dim SizeTxt As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
            If Me.Image Is Nothing Then
                g.DrawString(Me.Text, Me.Font, New SolidBrush(_theme.styleEnCours.foreColor), 10, (Me.Height - SizeTxt.Height) / 2)
            Else
                g.DrawString(Me.Text, Me.Font, New SolidBrush(_theme.styleEnCours.foreColor), 40, (Me.Height - SizeTxt.Height) / 2)
            End If
        End If

        'Dessin de l'icone éventuelle
        If (Not (Me.Image Is Nothing)) And (_nomTheme = nomThemeRadioButton.OMANE Or _BackColorEstDegrade) Then
            '----- Redimensionnement
            'nouveau size pour picturebox2
            'Dim grunit As GraphicsUnit = GraphicsUnit.Display
            'initTailleImage()
            g.DrawImage(Me.Image, 2, 2)
        End If

        'Tracé des bordures
        If _nomTheme = nomThemeRadioButton.OMANE Then
            g.DrawLine(New Pen(_theme.styleEnCours.borderColor, 1), 0, Me.Bounds.Height - 1, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
            g.DrawLine(New Pen(_theme.styleEnCours.borderColor, 1), 0, 0, 0, Me.Bounds.Height - 1)
            g.DrawLine(New Pen(_theme.styleEnCours.borderColor, 1), Me.Bounds.Width - 1, 0, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
        End If

        If _borderPerso Then
            If Me.Checked = True Then
                g.DrawLine(New Pen(_BorderColorChecked_Bottom), 0, Me.Bounds.Height - 1, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
                g.DrawLine(New Pen(_BorderColorChecked_Left, 1), 0, 0, 0, Me.Bounds.Height - 1)
                g.DrawLine(New Pen(_BorderColorChecked_top), 0, 0, Me.Bounds.Width - 1, 0)
                g.DrawLine(New Pen(_BorderColorChecked_Right), Me.Bounds.Width - 1, 0, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
            Else
                g.DrawLine(New Pen(_BorderColorNonChecked_Bottom), 0, Me.Bounds.Height - 1, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
                g.DrawLine(New Pen(_BorderColorNonChecked_Left, 1), 0, 0, 0, Me.Bounds.Height - 1)
                g.DrawLine(New Pen(_BorderColorNonChecked_top), 0, 0, Me.Bounds.Width - 1, 0)
                g.DrawLine(New Pen(_BorderColorNonChecked_Right), Me.Bounds.Width - 1, 0, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
            End If
        End If
    End Sub

#End Region

#Region "Interception des évènements"
    Private Sub PTIC_RadioButton_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.CheckedChanged
        If Checked = True Then
            If Not _theme Is Nothing Then
                _theme.etat = etatComposant.MouseOver
                Me.Invalidate()
            End If
        Else
            If Not _theme Is Nothing Then
                _theme.etat = etatComposant.Enabled
                Me.Invalidate()
            End If
        End If
    End Sub
#End Region

End Class
